package com.fast.develop.meta.resource.loader;

import cn.hutool.core.io.IoUtil;
import com.fast.develop.meta.base.loader.MetaLoader;
import com.fast.develop.meta.resource.Func;
import com.fast.develop.meta.resource.MetaResource;
import org.everit.json.schema.ObjectSchema;
import org.everit.json.schema.loader.SchemaClient;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 莫寅
 * @createTime 2022-06-04 13:31
 * @desc：
 */
public class MetaResourceLoader implements MetaLoader<MetaResource> {

    public static final String SCHEMA_PATH = "/.&&schema/resource/v1.0/resource_type.json";


    private ObjectSchema objectSchema;

    private ResourceFuncParser resourceFuncParser = new ResourceFuncParser();

    public MetaResourceLoader() {
        this.objectSchema = (ObjectSchema) SchemaLoader.builder()
                .schemaClient(SchemaClient.classPathAwareClient())
                .schemaJson(
                        new JSONObject(new JSONTokener(IoUtil.readUtf8(getClass().getResourceAsStream(
                                SCHEMA_PATH)))))
                .build().load().build();
    }

    public MetaResourceLoader(ObjectSchema objectSchema) {
        this.objectSchema = objectSchema;
    }

    @Override
    public void validation(JSONObject rawSchema){
        objectSchema.validate(rawSchema);
    }

    @Override
    public MetaResource load(JSONObject rawSchema){
        //检验json
        validation(rawSchema);

        MetaResource metaResource = new MetaResource();
        bindId(rawSchema, metaResource);
        bindType(rawSchema, metaResource);
        bindTitle(rawSchema, metaResource);
        bindIsSystem(rawSchema, metaResource);
        bindFuncs(rawSchema, metaResource);
        bindContentPanel(rawSchema, metaResource);
        bindpropertiesPanel(rawSchema, metaResource);

        return metaResource;
    }


    private void bindId(JSONObject rawSchema, MetaResource metaResource){
        metaResource.setId(rawSchema.optString("id"));
    }


    private void bindType(JSONObject rawSchema, MetaResource metaResource){
        String strType = rawSchema.optString("type");
        MetaResource.Type type = MetaResource.Type.create(strType);
        metaResource.setType(type);
    }

    private void bindTitle(JSONObject rawSchema, MetaResource metaResource){
        metaResource.setTitle(rawSchema.optString("title"));
    }

    private void bindIsSystem(JSONObject rawSchema, MetaResource metaResource){
        metaResource.setSystem(rawSchema.optBoolean("isSystem"));
    }


    private void bindFuncs(JSONObject rawSchema, MetaResource metaResource){
        JSONArray funcArray = rawSchema.optJSONArray("funcs");

        if(Objects.nonNull(funcArray)) {
            metaResource.setFuncs(funcArray.toList());
        }

    }


    private void bindContentPanel(JSONObject rawSchema, MetaResource metaResource){
        JSONObject jsonObject = rawSchema.optJSONObject("contentPanel");
        if(jsonObject != null){
            metaResource.setContentPanel(jsonObject.toMap());
        }
    }

    private void bindpropertiesPanel(JSONObject rawSchema, MetaResource metaResource){
        JSONObject jsonObject = rawSchema.optJSONObject("propertiesPanel");
        if(jsonObject != null){
            metaResource.setPropertiesPanel(jsonObject.toMap());
        }
    }

    public ObjectSchema getObjectSchema() {
        return objectSchema;
    }
}
